﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class AutocompleteHelpers
    {
        public static MvcHtmlString AutocompleteFor<T, TReturn>(
            this HtmlHelper<T> html,
            Expression<Func<T, TReturn>> prop,
            object value = null,
            string propId = null,
            object propIdValue = null,
            string controller = null,
            int? maxResults = null,
            string parentId = null,
            int? minLength = null,
            int? delay = null,
            object htmlAttributes = null,
            bool generatePropId = true,
            string prefix = null,
            IDictionary<string, string> data = null)
        {
            return Autocomplete(
                html, 
                ExpressionHelper.GetExpressionText(prop), 
                value, 
                propId, 
                propIdValue, 
                controller, 
                maxResults, 
                parentId, 
                minLength, 
                delay, 
                htmlAttributes,
                generatePropId,
                prefix,
                data);
        }

        public static MvcHtmlString Autocomplete(
            this HtmlHelper html,
            string prop,
            object value = null,
            string propId = null,
            object propIdValue = null,
            string controller = null,
            int? maxResults = null,
            string parentId = null,
            int? minLength = null,
            int? delay = null,
            object htmlAttributes = null,
            bool generatePropId = true,
            string prefix = null,
            IDictionary<string, string> data = null)
        {
            propId = propId ?? prop.MakePropId(Settings.Autocomplete.IdFormat);

            value = value.FromModel(html, prop);
            propIdValue = propIdValue.FromModel(html, propId);

            if (html.ViewData.ModelMetadata != null)
            {
                var vals = html.ViewData.ModelMetadata.AdditionalValues;
                controller = (string)controller.FromMeta("controller", vals);
                maxResults = (int?)maxResults.FromMeta("maxResults", vals);
                parentId = (string)parentId.FromMeta("parentId", vals);
                minLength = (int?)minLength.FromMeta("minLength", vals);
                delay = (int?)delay.FromMeta("delay", vals);
                propId = (string)propId.FromMeta("propId", vals);
            }

            controller = controller ?? prop.GetController() + "Autocomplete";

            var hd = htmlAttributes.GetDictionary();
            var h = !html.ViewData.ModelState.IsValidField(prop)
                        ? hd.AddVal("class", "input-validation-error")
                        : hd;
                
            var info = new AutocompleteInfo
                           {
                               Controller = controller,
                               Prop = prop,
                               Value = value,
                               PropId = propId,
                               PropIdValue = propIdValue,
                               MaxResults = maxResults ?? Settings.Autocomplete.MaxResults,
                               ParentId = parentId,
                               Delay = delay ?? Settings.Autocomplete.Delay,
                               MinLength = minLength ?? Settings.Autocomplete.MinLength,
                               HtmlAttributes = h.ToHtml(),
                               GeneratePropId = generatePropId,
                               Prefix = prefix,
                               Data = data
                           };

            return html.Partial(Settings.AwesomeFolder + "Autocomplete.ascx", info);
        }
    }
}